package com.cpi.cornpp.bushelversion.converter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.bushelversion.dto.BushelsVersionDTO;
import com.cpi.cornpp.bushelversion.exception.BushelsVersionException;
import com.cpi.cornpp.bushelversion.view.BushelsByCornOriginVersionView;
import com.cpi.cornpp.bushelversion.view.BushelsVersionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futurepurchase.view.CornOriginView;
import com.cpi.cornpp.plantversion.exception.PlantVersionException;
import com.cs.common.utils.NumberUtil;

/**
 * Bushels Version Converter
 * @author <i>Edgar Joao</i>
 * @version 1.0
 *
 */
public class BushelsVersionConverter {
	/**
	 * Bushels Version Converter DTO to View
	 * 
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 * @throws PlantVersionException
	 */
	public BushelsVersionView converterBushelsVersionDtoToView(BushelsVersionDTO dto) throws BushelsVersionException{
		BushelsVersionView view = null;
		try{
			if(dto != null){
				view = new BushelsVersionView();
				view.setBushelVersionId(dto.getBushelVersionId());
				view.setMonthPeriodId(dto.getMonthPeriodId());
				view.setAmount(NumberUtil.convertPrice(dto.getAmount()));
			}
		}catch( Exception exception ){
			BushelsVersionException bushelsVersionException=null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}	
		return view;
	}
	/**
	 * Bushels Version Converter View to DTO
	 * 
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 * @throws PlantVersionException
	 */
	public BushelsVersionDTO converterBushelsVersionViewToDto(BushelsVersionView view)throws BushelsVersionException{
		BushelsVersionDTO dto = null;
		try{
			if(view != null){
				dto = new BushelsVersionDTO();
				dto.setBushelVersionId(view.getBushelVersionId());
				dto.setMonthPeriodId(view.getMonthPeriodId());
				if(StringUtils.isNotEmpty(view.getAmount())){
					dto.setAmount(NumberUtil.parseDouble(view.getAmount()));
				}else{
					dto.setAmount(0);
				}
			}
		}catch( Exception exception ){
			BushelsVersionException bushelsVersionException=null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}	
		return dto;
	}
	/**
	 * Convierte Lista de DTOs a Views
	 * 
	 * @author Edgar Joao
	 * @param bushelsVersionsDTOs
	 * @return
	 * @throws BushelsVersionException
	 */
	public List convertDTOsToViews(List bushelsVersionsDTOs) throws BushelsVersionException{
		HashMap cornOrigins = null;
		Iterator iterator = null;
		BushelsVersionDTO bushelsVersionDTO = null;
		BushelsVersionView bushelsVersionView = null;
		BushelsByCornOriginVersionView bushelsByCornOriginVersionView = null;
		List bushelsByCornOriginVersionViews = null;
		CornOriginView cornOriginView = null;
		try{
			bushelsByCornOriginVersionViews = new ArrayList(0);
			cornOrigins = new HashMap();
			iterator = bushelsVersionsDTOs.iterator();
			while(iterator.hasNext()){
				bushelsVersionDTO = (BushelsVersionDTO) iterator.next();
				cornOriginView = new CornOriginView();
				cornOriginView.setId(bushelsVersionDTO.getCornOriginId());
				cornOriginView.setDescription(bushelsVersionDTO.getCornOrigindDescription());
				if( !cornOrigins.containsKey(cornOriginView.getId()) ){
					bushelsByCornOriginVersionView = new BushelsByCornOriginVersionView();
					bushelsByCornOriginVersionView.setCornOriginView(cornOriginView);
					cornOrigins.put(cornOriginView.getId(), bushelsByCornOriginVersionView);
					bushelsByCornOriginVersionViews.add(bushelsByCornOriginVersionView);
				}else{
					bushelsByCornOriginVersionView = (BushelsByCornOriginVersionView)cornOrigins.get(cornOriginView.getId());
				}
				bushelsVersionView = converterBushelsVersionDtoToView(bushelsVersionDTO);
				bushelsByCornOriginVersionView.getBushelsVersionViews().add(bushelsVersionView);
				
			}						
		}catch(Exception exception){
			BushelsVersionException bushelsVersionException=null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}		
		return bushelsByCornOriginVersionViews;
	}	
	/**
	 * Convierte Lista de Views a DTOs
	 * 
	 * @author Edgar Joao
	 * @param bushelsVersionsDTOs
	 * @return
	 * @throws BushelsVersionException
	 */
	public List convertViewsToDTOs(List bushelsVersionsViews) throws BushelsVersionException{
		List bushelsVersionDTOs = null;		
		Iterator iterator = null;
		BushelsVersionDTO bushelsVersionDTO = null;				
		BushelsVersionView bushelsVersionView = null;
		try{
			bushelsVersionDTOs = new ArrayList(0);
			iterator = bushelsVersionsViews.iterator();
			while(iterator.hasNext()){
				bushelsVersionView = (BushelsVersionView) iterator.next();
				bushelsVersionDTO = this.converterBushelsVersionViewToDto(bushelsVersionView);
				bushelsVersionDTOs.add(bushelsVersionDTO);
			}						
		}catch(Exception exception){
			BushelsVersionException bushelsVersionException=null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw bushelsVersionException;
		}		
		return bushelsVersionDTOs;
	}

}
